
import * as echarts from 'pages/ec-canvas/echarts';

const app = getApp()
Page({
  data: {
    ec: {
      onInit: initChart
    },
    userInfo:null       
  },
  onLoad: function () {
    var that = this;
    // 查看是否授权
    wx.getSetting({
      success: function (res) {
        if (res.authSetting['scope.userInfo']) {
          wx.getUserInfo({
            success: function (res) {
              console.log(res.userInfo)
              console.log(res.userInfo.nickName)
              that.setData({
                userInfo:res.userInfo
              })

              const userInfo2 = res.userInfo

              wx.login({
                success(res) {
                  if (res.code) {
                  console.log(res.code)
                    wx.request({
                      url: "http://47.106.145.141:8080/api/v1/login",
                      method: "POST",
                      data: {
                        'nickName': userInfo2.nickName,
                        'gender': userInfo2.gender,
                        'avatarUrl': userInfo2.avatarUrl,                       
                        'code': res.code
                      },
                      header: {
                        'content-type': 'application/x-www-form-urlencoded'
                      },
                      success: function (result) {
                      console.log(result.data);                               
                      app.globalData.key = result.data.dataInfo.token;
                      app.globalData.userId = result.data.dataInfo.id;
                      console.log(app.globalData.key);                                                           
                      }
                    })
                  } else {
                    console.log('登录失败！' + res.errMsg)
                  }
                }
              })
            }
          });
        } else {
          // 用户没有授权
          // 跳转授权页面
          wx.redirectTo({
            url: '../authorize/authorize',
          })
        }
      }
    });
  },

  // bindGetUserInfo: function (e) {
  //   if (e.detail.userInfo) {
  //     //用户按了允许授权按钮
  //     var that = this;
  //     // 获取到用户的信息了，打印到控制台上看下
  //     console.log("用户的信息如下：");
  //     console.log(e.detail.userInfo);
  //     //授权成功后,通过改变 isHide 的值，让实现页面显示出来，把授权页面隐藏起来
  //     that.setData({
  //       isHide: false
  //     });
  //   } else {
  //     //用户按了拒绝按钮
  //     wx.showModal({
  //       title: '警告',
  //       content: '您点击了拒绝授权，将无法进入小程序，请授权之后再进入!!!',
  //       showCancel: false,
  //       confirmText: '返回授权',
  //       success: function (res) {
  //         // 用户没有授权成功，不需要改变 isHide 的值
  //         if (res.confirm) {
  //           console.log('用户点击了“返回授权”');
  //         }
  //       }
  //     });
  //   }
  // },
  onGoodsTap: function (e) {
    wx.navigateTo({
      url: "function/goods/goods"
    })
  },
  onStorageTap: function (e) {
    wx.navigateTo({
      url: 'function/storage/storage',
    })
  },
  onPayTap:function(e){
    wx.navigateTo({
      url: 'function/pay/pay-scan/pay-scan',
    })
  }
})


function initChart(canvas,width,height) {
  const chart = echarts.init(canvas,null,{
    width:width,
    height:height
  });
  canvas.setChart(chart);


  var option = {
    color: ['#37a2da', '#32c5e9', '#67e0e3'],
    
    legend: {
      left: "50px",
      padding:"10rpx",
      
    },

    xAxis: {
      position: "bottom",
      type: 'category',
      data: ['4号', '5号', '6号', '7号', '8号', '9号', '10号'],
      
      
    },
    yAxis: {
      position:"left",
      type: 'value',
      name: "近六日营业额"
    },
    grid: {
      left: 50
    },
    series: [{
      data: [820, 932, 901, 934, 1290, 1330, 1320],
      type: 'line',
      smooth: true
    }]
  };


  chart.setOption(option);
  return chart;

}
